<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="UTF-8">
  <title>动态更新和删除列表项</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      background-color: #f4f4f4;
      margin: 0;
      padding: 20px;
    }

    h1 {
      text-align: center;
      color: #333;
    }

    ul {
      list-style-type: none;
      padding: 0;
      max-width: 600px;
      margin: 20px auto;
      background: white;
      border-radius: 5px;
      box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    }

    li {
      padding: 15px;
      border-bottom: 1px solid #ddd;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }

    li:last-child {
      border-bottom: none;
    }

    button {
      background-color: #007BFF;
      color: white;
      border: none;
      border-radius: 5px;
      padding: 8px 12px;
      cursor: pointer;
      transition: background-color 0.3s;
    }

    button:hover {
      background-color: #0056b3;
    }

    input[type="text"] {
      padding: 10px;
      width: 500px;
      border: 1px solid #ccc;
      border-radius: 5px;
      margin-right: 10px;
    }

    .input-content {
      display: flex;
      justify-content: center;
      margin-top: 20px;
    }
  </style>
</head>

<body>
  <h1>动态列表管理</h1>
  <ul id="myList"></ul>
  <div class="input-content">
    <input type="text" id="itemInput" placeholder="新项">
    <button id="addItem">添加项</button>
  </div>
  <script>
    const addItem = document.querySelector("#addItem")//获取id为'addItem'按钮对象
    const itemInput= document.querySelector("#itemInput")//获取输入对象
    const myList = document.querySelector("#myList")//获取列表对象

    //添加点击事件
    addItem.addEventListener("click", function(){
      const li = document.createElement("li")//创造行
      const button1 = document.createElement("button")//编辑按钮
      const button2 = document.createElement("button")//删除按钮
      const span = document.createElement("span")//文本

      span.innerHTML = itemInput.value
      button1.innerHTML = "编辑"
      button2.innerHTML = "删除"

      li.appendChild(span)//顺序加入
      li.appendChild(button1)
      li.appendChild(button2)

      button2.addEventListener("click", () => {
        li.remove()//删除
      })

      button1.addEventListener("click", () => {
        const content = prompt("请输入新内容", span.innerHTML)
        if (content !== null) {
          li.querySelector("span").innerHTML = content
        }
      })

      myList.appendChild(li)
      itemInput.value = ""//初始为空
    })


  </script>
</body>

</html>